﻿using System;
using System.ComponentModel;
using System.Drawing;
using Microsoft.WowAddonStudio.FrameXml.Components.Events;
using Microsoft.WowAddonStudio.FrameXml.Components.Support;
using Microsoft.WowAddonStudio.FrameXml.Serialization;
using Microsoft.WowAddonStudio.FrameXml.Serialization.LayoutFrames;

namespace Microsoft.WowAddonStudio.FrameXml.Components
{
	[ToolboxBitmap(typeof(System.Windows.Forms.CheckBox), "CheckBox.bmp")]
	[ToolboxItemFilter("WowAddonStudio", ToolboxItemFilterType.Require)]
    [EventGroup(EventGroup.CheckButton, EventName.OnClick)]
    public class CheckButtonControl : ButtonControl
    {
		/// <summary>
		/// Initializes a new instance of the <see cref="CheckButtonControl"/> class.
		/// </summary>
        public CheckButtonControl()
    	{
    	}

    	/// <summary>
        /// Initializes a new instance of the <see cref="CheckButtonControl"/> class.
        /// </summary>
        /// <param name="checkButton">The check button.</param>
        public CheckButtonControl(CheckButton checkButton)
            : base(checkButton)
        {
        }

        /// <summary>
        /// Gets the type of the serialization object this control manages.
        /// </summary>
        public override Type SerializationObjectType
        {
            get
            {
                return typeof(CheckButton);
            }
        }

        /// <summary>
        /// Gets the check button.
        /// </summary>
        public CheckButton CheckButton
        {
            get { return this.SerializationObject as CheckButton; }
        }

        /// <summary>
        /// Draws the FrameXML control.
        /// </summary>
        /// <param name="graphics">The <see cref="FrameXmlGraphics"/> context to draw to.</param>
        /// <param name="rectangle">The <see cref="Rectangle"/> to draw to.</param>
        public override bool DrawControl(FrameXmlGraphics graphics, Rectangle rectangle)
        {
            var drawControl = base.DrawControl(graphics, rectangle);
            if (!drawControl)
                return false;

            if (CheckButton.Checked && CheckButton.CheckedTexture != null)
                return graphics.DrawTexture(CheckButton.CheckedTexture, rectangle);

            return true;
        }

		/// <summary>
		/// Called when the <see cref="SerializationObject"/>'s property changes.
		/// </summary>
		/// <param name="e">A <see cref="PropertyChangedEventArgs"/> that contains the event data.</param>
        protected override void OnPropertyChanged(PropertyChangedEventArgs e)
        {
			base.OnPropertyChanged(e);

			switch (e.PropertyName)
			{
				case "Checked":
					// Invalidate control to force redraw
					this.Invalidate();
					break;
			}

			return;
        }

        /// <summary>
        /// Gets the default size of a FrameXML control.
        /// </summary>
        /// <value></value>
        protected override Size DefaultSize
        {
            get { return new Size(32, 32); }
        }
    }
}
